--
-- Author: shunguo.chen
-- Date: 2019-06-04 11:49:25
--
-- SkillLegendRender 神器技能render
--

local ClassRef = app.mvc.createRenderClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.SkillLegendIcon))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

-- data = Skill
function ClassRef:init(data)
    self.super.init(self, data)

    display.uiAddClick(self._ui.Panel, function()
        if self._data and not self:isChooseed() then
            executeFunc(self._onClick, self)
        end
    end)

    self._ui.Panel_skill:setTouchEnabled(false)
    self:setChooseed(false)

    self.stars = {}
    local starPanel = self._ui.Panel_star
    for i = 1, Constant.BattleComboPointMax do
        local star = {}
        star.bg = starPanel:getChildByName("bg_" .. i)
        star.star = starPanel:getChildByName("s_" .. i)
        star.bg:setVisible(false)
        star.star:setVisible(false)
        self.stars[i] = star
    end

    self:refresh()
end

function ClassRef:refresh()
    self:setVisible(self._data ~= nil)
    
    if self._data then
        local skill = self._data

        self._ui.name:setString(skill:getName())
        self._ui.icon:loadTexture(skill:getIcon())

        self:setComboPointCost(skill:getComboPoint())

        local costNum, costPath = skill:getCostByClass()
        local costColor = skill:getCostColorByClass()
        self:setManaCost(costNum, costColor)
    end
end

function ClassRef:isChooseed()
    return self._ui.bg_choose:isVisible()
end

function ClassRef:setChooseed(b)
    self._ui.bg_choose:setVisible(b)
end

function ClassRef:setManaCost(num, color)
    self._ui.icon_num:setVisible(num > 0)
    self._ui.num:setVisible(num > 0)
    if num > 0 then
        self._ui.num:setString(num)
        self._ui.num:setTextColor(color or display.COLOR_WHITE)
    end
end

function ClassRef:setComboPointCost(point)
    self._ui.Panel_star:setVisible(point > 0)
    if point == 0 then return end

    local showStars = {}
    if point == 1 then
        showStars[1] = self.stars[3]
    elseif point == 2 then
        showStars[1] = self.stars[2]
        showStars[2] = self.stars[4]
    elseif point == 3 then
        showStars[1] = self.stars[2]
        showStars[2] = self.stars[3]
        showStars[3] = self.stars[4]
    else
        showStars = table.clone(self.stars)
    end

    for i,star in ipairs(self.stars) do
        star.bg:setVisible(false)
        star.star:setVisible(false)
    end

    for i = 1, #showStars do
        local bShine = point >= i
        local star = showStars[i]
        star.bg:setVisible(bShine)
        star.star:setVisible(bShine)
    end
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
